# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 服务端方法3：TCP长连接.py
# @Author: dongguangwen
# @Date  : 2025-01-05 10:14
import socket


server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 12345))
server.listen(5)

print("Server is listening on port 12345...")

while True:
    conn, addr = server.accept()
    print(f"Connected by {addr}")
    while True:
        data = conn.recv(1024)
        if not data:
            print(f"Connection closed by {addr}")
            break
        print(f"Received: {data.decode()}")
        conn.sendall(f"Echo: {data.decode()}".encode())
